package cn.qidd.keep.order.controller;

import cn.qidd.keep.domain.core.Result;
import cn.qidd.keep.domain.core.dict.MemberStatus;
import cn.qidd.keep.domain.core.dict.WhetherStatus;
import cn.qidd.keep.domain.document.Member;
import cn.qidd.keep.domain.document.Order;
import cn.qidd.keep.domain.web.BaseController;
import cn.qidd.keep.domain.web.BusinessException;
import cn.qidd.keep.domain.web.UserVO;
import cn.qidd.keep.order.feignclient.MemberServiceClient;
import cn.qidd.keep.order.model.order.CreateOrderRequest;
import cn.qidd.keep.order.model.order.OrderSearchParam;
import cn.qidd.keep.order.repository.OrderRepository;
import cn.qidd.keep.order.service.OrderService;
import com.querydsl.core.BooleanBuilder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

import static cn.qidd.keep.domain.core.Constants.TOKEN;

@RestController
@RequestMapping("/api/order")
@Api(value = "OrderController", description = "订单相关操作")
public class OrderController extends BaseController {

    @Resource
    private OrderRepository orderRepository;
    @Resource
    private OrderService orderService;

    @GetMapping("/findOrderListByPage")
    @ApiOperation(value = "分页查询订单", notes = "分页查询订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", defaultValue = "0", dataType = "integer", paramType = "query",
                    value = "页数 (1..N)"),
            @ApiImplicitParam(name = "pageSize", defaultValue = "10", dataType = "integer", paramType = "query",
                    value = "每页大小"),
            @ApiImplicitParam(name = "sort", dataType = "string", paramType = "query",
                    value = "依据什么排序: 属性名1 asc,属性名2 desc. ")
    })
    public Result findOrderListByPage(Pageable pageable, OrderSearchParam param) {
        logger.debug("Rest request to findOrderListByPage: {} ", param);
        BooleanBuilder builder = param.generateSearchCondition();
        Page<Order> page = orderRepository.findAll(builder, pageable);
        return new Result<>(Result.Status.SUCCESS, page);
    }

    @PostMapping("/createOrder")
    @ApiOperation(value = "创建订单", notes = "创建订单")
    public Result createOrder(@ApiIgnore @RequestHeader(TOKEN) String token, @Validated @RequestBody CreateOrderRequest request) {
        logger.debug("Rest request to create order: {}", request);
        request.valid();
        UserVO userVO = getUserByToken(token);
        orderService.createOrder(userVO, request);
        return new Result(Result.Status.SUCCESS, "创建成功");
    }
}
